package com.shujujiegou.链表.单向链表;

import lombok.Data;

public class SingleLinkedList {
    private int size;
    private Node head;

    public SingleLinkedList() {
        this.size = 0;
        this.head = null;
    }

    //在链表头添加元素
    public void addHead(Object data) {
        Node node = new Node(data);
        if(isEmpty()) {
            node.setNext(null);
        } else {
            node.setNext(this.head);
        }

        this.head = node;
        this.size ++;
    }

    // 在链表头删除元素
    public Object removeHead() {
        Object target = null;

        if(!isEmpty()) {
            target = this.head.data;
            this.head = this.head.next;
            this.size --;
        }

        return target;
    }

    // 查找指定元素，找到了返回节点Node，找不到返回null
    public Object find(Object data) {
        Node target = null;
        if(!isEmpty()) {
            int temp = this.size;
            Node currentNode = this.head;
            while (temp > 0 && null != currentNode) {
                if(data.equals(currentNode.data)) {
                    target = currentNode;
                    break;
                }

                currentNode = currentNode.next;
                temp--;
            }
        }

        return target;
    }

    // 删除指定的元素，删除成功返回true
    public Boolean remove(Object data) {
        Boolean removeSuccessa = false;
        if(!isEmpty()) {
            Node current = this.head;
            Node previous = this.head;

            while(current.getData() != data) {
                if(current.next == null) {
                    return false;
                } else {
                    current = current.next;
                    previous = current;
                }
            }

            if(current == head) {
                this.head = current.next;
            } else {
                previous.next = current.next;
            }

            this.size--;
            removeSuccessa = true;
        }

        return removeSuccessa;
    }

    // 判断链表是否为空
    public Boolean isEmpty() {
        if(this.size <= 0) {
            return true;
        }

        return false;
    }

    // 显示节点信息
    public void display() {
        if(size >0){
            Node node = head;
            int tempSize = size;
            if(tempSize == 1){//当前链表只有一个节点
                System.out.println("["+node.data+"]");
                return;
            }
            while(tempSize>0){
                if(node.equals(head)){
                    System.out.print("["+node.data+"->");
                }else if(node.next == null){
                    System.out.print(node.data+"]");
                }else{
                    System.out.print(node.data+"->");
                }
                node = node.next;
                tempSize--;
            }
            System.out.println();
        }else{//如果链表一个节点都没有，直接打印[]
            System.out.println("[]");
        }
    }

    @Data
    public class Node{
        private Object data; // 每个节点的数据
        private Node next; // 指向下一个节点的连接

        public Node(Object data) {
            this.data = data;
        }
    }
}
